package com.wjb.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.rabbitmq.client.Channel;
import com.wjb.common.BaseResultString;
import com.wjb.pojo.vo.OrderxVo;
import com.wjb.service.PlaceOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import com.alibaba.fastjson.JSON;
import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * Order订单类导出监听器
 * @author : 王济彬
 * @date: 2025/5/25 - 05 - 25 - 17:34
 * @Description: com.wjb.listener
 * @version: 1.0
 */
@Slf4j
public class OrderListener extends ActionExcelAnalysisEventListener<OrderxVo>{

    //有效条数
    private int validCount;
    //无效条数
    private int invalidCount;

    StringBuilder msg = new StringBuilder();

    private PlaceOrderService  placeOrderService;
    public OrderListener(PlaceOrderService placeOrderService) {
        this.placeOrderService = placeOrderService;
    }

    @Override
    public void invoke(OrderxVo orderxVo, AnalysisContext analysisContext) {
        log.info("解析到需要导入的一条用户数据：{}",JSON.toJSONString(orderxVo));

        //校验数据
        StringBuilder vaildMsg = new StringBuilder();
        if(!StringUtils.isEmpty(String.valueOf(vaildMsg))){
            boolean isSuccess = true; //此处应该是相关的校验逻辑，如调用save方法等
            if(isSuccess){
                validCount++;
            }else{
                invalidCount++;
                msg.append("第"+(validCount+invalidCount)+"行数据校验失败。 <br/>");
            }
        }else{
            invalidCount++;
            msg.append("第"+(validCount+invalidCount)+"行数据校验失败：").append(vaildMsg+"<br/>");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("所有数据已经校验完毕");
    }

    @Override
    public String getMsg() {
        return String.format("导入操作结束：成功条数：%d,失败条数：%d,错误信息：{}", validCount, invalidCount, msg);
    }
}
